<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/"> </el-breadcrumb>
        </div>
        <el-table :data="tableData.records">
            <el-table-column type="expand">
                <template slot-scope="scope">
                    <div v-html="scope.row.text"></div>
                </template>
            </el-table-column>
            <el-table-column type="index" :index="indexMethod"></el-table-column>
            <el-table-column label="id" prop="id" sortable></el-table-column>
            <el-table-column label="用户" prop="username" sortable></el-table-column>
            <el-table-column label="邮箱" prop="email"></el-table-column>
            <el-table-column label="标题" prop="title"></el-table-column>
            <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                         <el-button
                            type="text"
                            icon="el-icon-message"
                            @click="handleAnswer(scope.$index, scope.row)"
                        >回复</el-button>
                    </template>
            </el-table-column>
        </el-table>

        <div class="pagination">
            <el-pagination
                style="margin-top: 10px"
                background
                :current-page.sync="query.pageNum"
                :page-count="tableData.pages"
                layout="prev,pager,next"
                @current-change="getData"
            ></el-pagination>
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog title="回复窗口" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.username" disabled></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="form.email" disabled></el-input>
                </el-form-item>
                <el-form-item label="回复内容">
                    <el-input type="textarea" :rows="4" placeholder="请输入回复内容" v-model="form.reply">
                    </el-input>
                </el-form-item>
                
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
import request from '@/utils/request';
import qee from 'qf-export-excel';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                pageNum: 1,
                pageSize: 8
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            formLabelWidth: '120px',
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
           request.get("http://localhost:8081/advice/getAll",{
               params:this.query
        }).then(res=>{
            console.log(res)
            this.tableData = res.data
        })
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    let form = new FormData();
                    form.append('id', row.id);
                    request.post('http://localhost:8081/advice/del', form).then((res) => {
                        this.$message.success('删除成功');
                        this.getData();
                    });
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            const userids = [];
            this.delList.forEach((item) => {
                userids.push(item.userid);
            });
            request
                .post('http://localhost:8081/User/mulDel', this.qs.stringify({ ids: userids }, { arrayFormat: 'repeat' }))
                .then((res) => {
                    this.$message.success('删除成功');
                    this.getData();
                });
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            console.log(this.form)
            let params = new FormData();
            params.append('username',this.form.username);
            params.append('email',this.form.email);
            params.append('reply',this.form.reply);
            request.post('http://localhost:8081/advice/reply', params).then((res) => {
                this.$message.success('回复成功');
                this.editVisible = false;
                this.getData();
            });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        downExecl() {
            const titleList = [
                { title: '用户名', key: 'userid' },
                { title: '邮箱', key: 'email' },
                { title: '手机号', key: 'tel' },
                { title: '注册时间', key: 'date' }
            ];
            qee(titleList, this.tableData, 'users');
        },
        indexMethod(index) {
            return index + 1;
        },
        handleAnswer(index, row){
            this.form = row;
            this.editVisible = true;
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 200px;
    display: inline-block;
    margin-left: 20px;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.yellow {
    color: rgb(136, 138, 18);
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
